import cherrypy
import os
import glob
import yaml
import json
from jinja2 import Environment, FileSystemLoader

import manage

env = Environment(loader=FileSystemLoader('web'), autoescape=True)

class Inventory:
    @cherrypy.expose
    def index(self, filter=None):
        page = env.get_template('index.htm')
        #hostname ip interfaces (name, mac) os key
        computers = manage.Computer.all()
        return page.render(list=computers)

class Packages:
    exposed = True
    
    @cherrypy.expose
    def index(self):
        page = env.get_template('package_list.htm')
        packages = manage.Package.all()
        return page.render(list=packages)
    
    @cherrypy.expose
    def new(self):
        page = env.get_template('package_new.htm')
        return page.render()
    
    @cherrypy.expose
    def new_do(self, name, desc):
        ident = manage.new_package(name, desc)
        raise cherrypy.HTTPRedirect('/packages/details/%s' % ident)

Inventory.packages = Packages()

class ClientAPI:
    exposed = True
    
    @cherrypy.expose
    def register(self, password):
        cookie = manage.register(password)
        return cookie
    
    @cherrypy.expose
    def ping(self, cookie, data):
        manage.ping(cookie, json.loads(data))
        return cookie

Inventory.client = ClientAPI()
